package com.runen.infinitemovement.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by LiaoDuanHong
 * Create on 2020/09/14 10:42
 */
public class MySuggestActivity extends BaseActivity {
    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.edt_input_suggest)
    EditText edtInputSuggest;
    @BindView(R.id.edt_input_phone_or_qq)
    EditText edtInputPhoneOrQq;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_suggest;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("意见反馈");
    }

    @OnClick({R.id.top_btn_left, R.id.tv_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.tv_submit:
                submit();
                break;
        }
    }

    private void submit() {
        String contact = edtInputPhoneOrQq.getText().toString().trim();
        String content = edtInputSuggest.getText().toString().trim();
        if (TextUtils.isEmpty(contact)) {
            ToastUtils.showShort("手机号码或者邮箱不能为空！！！");
            return;
        }
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort("反馈内容不能为空！！！");
            return;
        }
        JSONObject object = new JSONObject();
        try {
            object.put("contact", contact);
            object.put("content", content);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        showDialog();
        new UserAPI().suggestInfo(object.toString(), new BaseResultCallback<BaseResponseEntity<?>>() {
            @Override
            public void onResponse(BaseResponseEntity<?> response) {
                dismissDialog();
                if (response.getCode() == 0) {
                    finish();
                } else {
                    ToastUtils.showShort(response.getMessage());
                }
            }

            @Override
            public void onError(Throwable e, String body) {
                super.onError(e, body);
                dismissDialog();
            }
        });
    }
}
